﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LabManager.UserControls.Tabs.Templates;
using DataConnector;
using Library;
using Equin.ApplicationFramework;
using System.Data.Entity;

namespace LabManager.UserControls.Tabs.Masterdata
{
    public partial class TabEnvironment : TabTemplate
    {
        private BindingListView<LabEnvironment> bindingList;
        private BindingSource bindingSource = new BindingSource();
        private int selectedID = -1;

        public TabEnvironment()
        {
            InitializeComponent();
        }

        public override void Load()
        {
            SetTexts();
            LoadLabEnvironmentList();
        }

        private void SetTexts()
        {
            //TODO
        }

        private void LoadLabEnvironmentList()
        {
            bindingList = new BindingListView<LabEnvironment>(DataConnectorLayer.Instance.R_LabEnvironment.FindAll(x => x.LabEnvironmentID > 0).ToList<LabEnvironment>());
            bindingSource.DataSource = bindingList;
            dgMain.DataSource = bindingSource;
            bnNavi.BindingSource = bindingSource;

            if (dgMain.Rows.Count > 0)
            {
                selectedID = (int)dgMain.Rows[0].Cells[0].Value;
            }

            dgMain_SelectionChanged(null, null);
        }

        #region Events

        private void dgMain_SelectionChanged(object sender, EventArgs e)
        {
            if (dgMain.SelectedRows.Count > 0)
            {
                var selectedRow = dgMain.SelectedRows[0];

                selectedID = (int)selectedRow.Cells[0].Value;
                tbLabEnvironment.Text = selectedRow.Cells[1].Value.ToString();

                // Delete button
                var canDelete = DataConnectorLayer.Instance.R_LabEnvironmentProductType.FindOne(x => x.LabEnvironment.LabEnvironmentID == selectedID) == null;
                btDelete.Enabled = canDelete;
                btUpdate.Enabled = true;
            }
            else
            {
                tbLabEnvironment.Text = string.Empty;

                btUpdate.Enabled = false;
                btDelete.Enabled = false;
            }
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            var item = new LabEnvironment() { LabEnvironmentName = Helper.EMPTY_STRING };
            DataConnectorLayer.Instance.R_LabEnvironment.Create(item);
            DataConnectorLayer.Instance.R_LabEnvironment.SaveChanges();
            LoadLabEnvironmentList();
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_LabEnvironment.FindById(selectedID);
            item.LabEnvironmentName = tbLabEnvironment.Text != "" ? tbLabEnvironment.Text : Helper.EMPTY_STRING;
            DataConnectorLayer.Instance.R_LabEnvironment.Update(item);
            DataConnectorLayer.Instance.R_LabEnvironment.SaveChanges();

            LoadLabEnvironmentList();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_LabEnvironment.FindById(selectedID);
            DataConnectorLayer.Instance.R_LabEnvironment.Delete(item);
            DataConnectorLayer.Instance.R_LabEnvironment.SaveChanges();

            LoadLabEnvironmentList();
        }

        private void tbFilter_TextChanged(object sender, EventArgs e)
        {
            var search = tbFilter.Text.ToLower();

            bindingList.ApplyFilter(delegate(LabEnvironment labEnvironment)
            {
                return labEnvironment.LabEnvironmentName.ToLower().Contains(search);
            });
        }

        #endregion

    }
}